package com.loma.common.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.gyf.immersionbar.ImmersionBar
import com.loma.common.R
import com.loma.common.utils.ext.showToast

/***
 * 使用databinding的activity抽像类
 */
abstract class FragmentBase<VM : ViewModelBase, B : ViewDataBinding> : Fragment() {
    val vm by lazy { ViewModelProvider(this).get(vmClass) }
    abstract val layoutId: Int
    abstract val vmClass: Class<VM>
    private lateinit var progressDialogFragment: ProgressDialogFragment

    lateinit var binding: B
    private var lazyLoaded = false
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(layoutId, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding = DataBindingUtil.bind<B>(view)!!
        initView()
        observe()
    }

    open fun initView() {}

    override fun onResume() {
        super.onResume()
        // 实现懒加载
        if (!lazyLoaded) {
            lazyLoadData()
            lazyLoaded = true
        }

    }


    abstract fun lazyLoadData()


    open fun observe() {
        vm.run {
            exception.observe(viewLifecycleOwner, Observer {
                it.message?.let { msg->
                    requireContext().showToast(msg)
                }

            })
            showProgress.observe(viewLifecycleOwner, Observer {
                if (it) showProgressDialog() else hideProgressDialog()
            })

        }

    }


    fun showProgressDialog() {

        if (!this::progressDialogFragment.isInitialized) {
            progressDialogFragment =
                ProgressDialogFragment.Companion.newInstance()
        }
        progressDialogFragment.show(
            childFragmentManager,
            R.string.waiting, true
        )
    }

    fun hideProgressDialog() {
        if (this::progressDialogFragment.isInitialized && progressDialogFragment.isVisible) {
            progressDialogFragment.dismiss()
        }
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    /**謤
     * 设置沉浸式状态栏
     * @param colorResId 状态栏颜色
     */

    protected fun initImmersiveStatusBar() {

        ImmersionBar.with(this).statusBarDarkFont(true).fitsSystemWindows(true)
            .statusBarColor(R.color.white).init()

    }


    /**謤
     * 设置沉浸式状态栏
     * @param colorResId 状态栏颜色
     */

    fun initImmersiveStatusBar(color: Int, isDarkFont: Boolean) {

        ImmersionBar.with(this).statusBarDarkFont(isDarkFont).fitsSystemWindows(true)
            .statusBarColor(color).init()

    }

}
